@file:Suppress("NOTHING_TO_INLINE")

package com.hzw.core.resources

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Build
import android.view.View
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.fragment.app.Fragment
import com.hzw.core.ctx.appCtx

@ColorInt
fun Context.color(@ColorRes colorRes: Int): Int =
    if (Build.VERSION.SDK_INT >= 23) getColor(colorRes) else {
        @Suppress("DEPRECATION")
        resources.getColor(colorRes)
    }

inline fun Fragment.color(@ColorRes colorRes: Int) = context!!.color(colorRes)
inline fun View.color(@ColorRes colorRes: Int) = context.color(colorRes)
inline fun appColor(@ColorRes colorRes: Int) = appCtx.color(colorRes)


fun Context.colorSL(@ColorRes colorRes: Int): ColorStateList {
    return (if (Build.VERSION.SDK_INT >= 23) getColorStateList(colorRes) else {
        @Suppress("DEPRECATION")
        resources.getColorStateList(colorRes)
    })
}

inline fun Fragment.colorSL(@ColorRes colorRes: Int) = context!!.colorSL(colorRes)
inline fun View.colorSL(@ColorRes colorRes: Int) = context.colorSL(colorRes)
inline fun appColorSL(@ColorRes colorRes: Int) = appCtx.colorSL(colorRes)


private inline val defaultColor get() = Color.WHITE
@ColorInt
fun Context.styledColor(@AttrRes attr: Int): Int = withStyledAttributes(attr) { getColor(it, defaultColor) }
inline fun Fragment.styledColor(@AttrRes attr: Int) = context!!.styledColor(attr)
inline fun View.styledColor(@AttrRes attr: Int) = context.styledColor(attr)
inline fun appStyledColor(@AttrRes attr: Int) = appCtx.styledColor(attr)

fun Context.styledColorSL(@AttrRes attr: Int): ColorStateList? = withStyledAttributes(attr) { getColorStateList(it) }
inline fun Fragment.styledColorSL(@AttrRes attr: Int) = context!!.styledColorSL(attr)
inline fun View.styledColorSL(@AttrRes attr: Int) = context.styledColorSL(attr)
inline fun appStyledColorSL(@AttrRes attr: Int) = appCtx.styledColorSL(attr)
